<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 实体模式 春招!!!!!!!!!!!!!!!!!
        // 访问器属性
        // set get 方法访问时不用加()
        var obj = {
            _x: 100,
            get x() {
                return this._x;
            },
            set x(value) {
                this._x = value;
            }
        }
        console.log(obj.x) //set方法
        obj.x = 200; //get方法
        console.log(obj.x) //set方法

        //定义方式1
        var obj = {
            price: 1000,
            set nowPrice(value) {
                this.price = value;
            },
            get nowPrice() {
                return this.price * 0.8;
            }
        };
        //定义方式2
        var obj = {price:1000};
        Object.defineProperty(obj, "nowPrice", {
            set: function(value) {
                this.price = value;
            },
            get: function() {
                return this.price * 0.8;
            },
            configurable: false,
            enumerable: true
        });

        var salary = {
            arr: [],
            set wage(value) {
                if (typeof value === "number" && !isNaN(value)) {
                    this.arr.push(value);
                } else {
                    alert("输入有误，请改正");
                }
            },
            get wage() {
                var sum = 0;
                for (var i = 0; i < this.arr.length; i++) {
                    sum += this.arr[i];
                }
                return sum;
            }
        };
        typeof salary.wage //get方法 返回结果的数据类型 number
        salary.wage = 100; //set方法
        salary.wage = 200; //set方法
        salary.wage = "sss"; //set方法
        salary.wage;   //get方法

    </script>
</body>
</html>